package com.hc.dfs.activity;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

import com.hc.dfs.activity.R;
import com.hc.dfs.service.MainService;
import com.hc.dfs.tool.Tool;

/** 
 * @author 王洪胜  E-mail:  
 * @version 创建时间：2013-2-17 下午4:14:31
 * 
 * 类说明：
 *
 */
public class MainActivity extends ActivityGroup implements OnTabChangeListener{
	//顶部栏
	private View header;
	private TextView topTitle;
	//顶部标题
	private int[] titles = new int[]{R.string.manage, R.string.search, R.string.contact, R.string.settings};
	//标签要切换的Activity的Class
	private Class<?>[] tabCls = new Class<?>[]{
			ManageActivity.class,
			SearchActivity.class,
			ContactActivity.class,
			SettingsActivity.class
	};
	//标签按钮背景
	private int[] buttonBG = new int[]{
			R.drawable.button_manage,
			R.drawable.button_search,
			R.drawable.button_contact,
			R.drawable.button_settings
	};
    @Override
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.main);
        Bundle bd = new Bundle();
        Intent itt = getIntent();
        bd = itt.getBundleExtra("unsigned");
        initLayout();
        System.out.println("主线程的id是："+Thread.currentThread().getId());
    }
    
    
@Override
	protected void onResume() {
		super.onResume();
		//判断网络是否正常，并初始化服务线程
		init();
	}


	//    @Override
//    protected void onPause() {
//    	unregisterReceiver(modifyFuncButton);
//    	super.onPause();
//    }
//    @Override
//    protected void onResume() {
//    	super.onResume();
//    	 IntentFilter filter = new IntentFilter();
//    	 //向过滤器中添加action
//    	 filter.addAction("com.hc.dfs.displayButton");
//    	  //注册广播
//         registerReceiver(modifyFuncButton, filter);
//
//    }
//	//接收广播信息
//	private BroadcastReceiver modifyFuncButton = new BroadcastReceiver() {
//		
//		@Override
//		public void onReceive(Context context, Intent intent) {
//			System.out.println("guangbo@!@@@@@@@@@@");
//			funcButton.setText("选择日期");
//			funcButton.setVisibility(View.VISIBLE);
//		}
//	};


	/**
	 * Activity布局初始化方法
	 */
    private void initLayout() {
    	//获取顶部标题控件
    	header = (View) findViewById(R.id.header);
    	topTitle = (TextView) header.findViewById(R.id.textView1);
    	//从布局文件获取TabHost，必须加上setup()方法
    	TabHost tabhost = (TabHost) findViewById(R.id.tabhost); 
    	tabhost.setup();
    	//获取管理多个Activity的权限
    	tabhost.setup(this.getLocalActivityManager());
		//向标签控件循环添加新标签
    	for(int i=0; i<tabCls.length; i++){
			setIndicator(buttonBG[i], i, new Intent(this, tabCls[i]), tabhost);
		}
		//设置初始的标签
		tabhost.setCurrentTab(0); 
		topTitle.setText(titles[0]);
		//设置标签切换监听器
		tabhost.setOnTabChangedListener(this);
	}

    /**
     * 初始化标签的方法
     * @param icon
     * @param tabId
     * @param intent
     * @param tabhost
     */
	private void setIndicator(int icon, int tabId, Intent intent, TabHost tabhost) {
		//从tabHost的上下文环境中获取一个标签按钮的布局文件
		View localView = LayoutInflater.from(tabhost.getContext())
				.inflate(R.drawable.tab_widget, null);
		//设置标签按钮的背景
		localView.setBackgroundResource(icon);
		//创建新标签，添加标签tag、标签按钮布局、标签对应的Activity
		TabHost.TabSpec localTabSpec = tabhost.newTabSpec(String.valueOf(tabId))
				.setIndicator(localView).setContent(intent);
		//新标签添加到标签栏
		tabhost.addTab(localTabSpec);
	}

	@Override
	public void onTabChanged(String tabId) {
		// TODO Auto-generated method stub
		//切换标签时更新顶部标题
		for(int i=0; i<titles.length; i++){
			if(i==Integer.parseInt(tabId)){
				topTitle.setText(titles[i]);
			}
		}
	}
	
	/**
	 * 初始化方法
	 */
	public void init(){
		//获取网络状态
        Boolean chkNet = Tool.checkNetWorkStatus(this);
        if(chkNet){
        	//网络正常则启动主服务线程
    		Intent startService = new Intent(this, MainService.class);
    		this.startService(startService);
        }else{
        	Tool.alerNetErr(this);
        	return;
        }
		
	}	
	
	/**
	 * 监听返回键Activity通过onKeyDown方法，ActivityGroup则通过dispatchKeyEvent方法
	 */
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {

		if(event.getKeyCode()==KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
			Tool.promptExit(this);
	    	return false;
	    }
	    return super.dispatchKeyEvent(event);
	}

}
